package com.its.myalgorithm.day01;

/**
 * @projectName: dgl-algorithm
 * @package: com.its.myalgorithm.day01
 * @className: Code02_SumOfFactorial
 * @author: DONG GL
 * @description: TODO 阶乘和 给定一个参数N，返回1!+2!+3!+4!+…+N!的结果
 * @date: 2022/3/29 17:55
 * @version: 1.0
 */
public class SumOfFactorial {
    /**
     * 返回 1!+2!+3!+4!+…+N!
     * n=1  返回 1  ans = 1 | cur=1!
     * n=2  返回 ans = ans + cur * 2  ; cur = 2! = 上一步的 cur * 2
     * n=3  返回 ans = ans + cur * 3  ; cur = 3! = 上一步的 cur * 3
     * n=4  返回 ans = ans + cur * 4 ; cur = 4! = 上一步的 cur * 3
     * n=5  返回
     * @param N
     * @return
     */
    public static long f2(int N) {
        long cur = 1;
        long ans = 0;
        for (int i = 1; i <= N; i++) {
            cur = cur * i;
            ans += cur;
        }
        return ans;
    }


    public static long f1(int N) {
        long sum = 0;
        for (int i = 1; i <= N; i++) {
            long ans = 1;
            for (int j = 1; j <= i; j++) {
                ans *= j;
            }
            sum += ans;
        }
        return sum;

    }


    public static void main(String[] args) {
        int n = 5;
        System.out.println(f1(n));
        System.out.println(f2(n));
    }
}
